package lesson04;

/**
 * @Author 比特就业课
 * @Date 2023-01-06
 */
public class Demo_410 {
    private static int num = 50000;

    public static void main(String[] args) throws InterruptedException {

        Counter10 counter  = new Counter10();
        Counter10 counter1  = new Counter10();

        // 创建线程完成自增
        // 创建两个线程
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < num; i++) {
                // 这里调用了counter1的自增方法
                counter.increase();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < num; i++) {
                // 这里调用了counter2的自增方法
                counter1.increase();
            }
        });
        // 启动线程
        t1.start();
        t2.start();
        // 等待两个线程执行完成
        t1.join();
        t2.join();

        // 获取自增后的count值
        System.out.println("count结果 = " + counter.count);

    }
}

class Counter10 {
    public static int count = 0;

    public void increase() {
        // 使用这个类对象持有锁信息
        synchronized (Counter10.class) {
            count++;
        }
    }

}
